/*
 * ItemRentBOImpl.java
 *
 * Created on July 7, 2007, 2:36 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.abbh.libcommunity.bo.impl;
import com.abbh.libcommunity.bo.ItemRentBO;
import com.abbh.libcommunity.dao.ItemRentDAO;
import com.abbh.libcommunity.dao.impl.*;
import com.abbh.libcommunity.model.Item;
import com.abbh.libcommunity.model.User;
import com.abbh.libcommunity.model.UserHoldItems;
import com.abbh.libcommunity.model.UserRentedItems;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 *
 * @author PAUL
 */
public class ItemRentBOImpl implements ItemRentBO {
    
    /** Creates a new instance of ItemRentBOImpl */
    public ItemRentBOImpl() {
    }
    public   void rentItem(User user,Item item) {
        ItemRentDAOImpl irdi=new ItemRentDAOImpl();
        irdi.rent(user,item);
        
        
    }
    
    public void renewItem(Integer itemId){
        
        ItemRentDAO itemRentedDAO = new ItemRentDAOImpl();
        
        UserRentedItems userRentedItems = itemRentedDAO.findByItemId(itemId);
        int renews = userRentedItems.getRenew();
        renews++;
        userRentedItems.setRenew(renews);
        
        // upating due date
        Date date = userRentedItems.getDueDate();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 15);
        userRentedItems.setDueDate(calendar.getTime());
        
        itemRentedDAO.update(userRentedItems);    
        
    }
    
    public void returnItem(UserHoldItems item){
        
        ItemRentDAOImpl x = new ItemRentDAOImpl();
        x.returns( item);
    }
    
    public  UserRentedItems  findByItemId(Integer  id){
        
        ItemRentDAOImpl x = new ItemRentDAOImpl();
        return x.findByItemId(id);
        
    }
}
